import javax.swing.*;
import java.io.File;
import java.io.RandomAccessFile;

public class VaultFile {
    static final int _MIN_HEADER_LENGTH = 86;
    String vaultpath, temppath;
    RandomAccessFile fsvault;
    short version; // UInt16
    int vault_size, header_size; // UInt32

    byte[] header;
    boolean flag_new;

    int get_vault_size() {
        return vault_size;
    }

    int get_vault(byte[] evaultdata) {
        try {
            fsvault.seek(header_size);
            if (fsvault.read(evaultdata, 0, vault_size) < (int)vault_size) return (int) PasswordManagerError.NL_STATUS_BADFILE;
        }
        catch (Exception e) {
            return (int) PasswordManagerError.NL_STATUS_UNKNOWN;
        }

        return (int) PasswordManagerError.NL_STATUS_OK;
    }


    int open_read(String path) {
        int rv;
        RandomAccessFile fs;

//        JOptionPane.showMessageDialog(null, "hehe");
//        return 0;

        flag_new = false;

        if (path == null || path.isEmpty() || path.isBlank()) return (int)PasswordManagerError.NL_STATUS_BADFILE;

        try {
            fs = new RandomAccessFile(path, "rw");
            if (fs.length() < header_size)
                return (int) PasswordManagerError.NL_STATUS_BADFILE;
        }
////        catch (UnauthorizedAccessException ^) { return PasswordManagerError.NL_STATUS_PERM; }
////	catch (ArgumentNullException ^) { return PasswordManagerError.NL_STATUS_INVALID; }
////	catch (ArgumentException ^) { return PasswordManagerError.NL_STATUS_INVALID; }
////	catch (PathTooLongException ^) { return PasswordManagerError.NL_STATUS_SIZE; }
////	catch (DirectoryNotFoundException ^) { return PasswordManagerError.NL_STATUS_NOTFOUND; }
////	catch (NotSupportedException ^) { return PasswordManagerError.NL_STATUS_INVALID; }
    	catch (Exception e) { return (int) PasswordManagerError.NL_STATUS_UNKNOWN; }

        // If we had a vault open before, close it and use the new file stream.

        this.close(fsvault);
        vaultpath = path;
        fsvault = fs;

        // Read the header

        rv = this.read_header();
        if (rv != PasswordManagerError.NL_STATUS_OK) {
            this.close(fs);
        }

        return rv;

    }

    void close(RandomAccessFile stream)
    {
        try {
            stream.close();
        }
        catch (Exception e) {
        }

        // If we didn't write anything to the file, delete it.

        try {
            File f = new File(vaultpath);
            if (f.length() == 0 ) {
                f.delete();
            }
        }
        catch (Exception e) {}
    }

    byte[] get_header() {
        return header;
    }

    short get_header_size() {
        return (short)header_size;
    }

    int read_header() {
//        try {
//            fsvault->Seek(0, SeekOrigin::Begin);
//            if (fsvault->Read(header, 0, header_size) < (int)header_size) return NL_STATUS_BADFILE;
//
//            version= BitConverter::ToUInt16(header, 52);
//            if (version != 1) return NL_STATUS_VERSION;
//
//            vault_size = BitConverter::ToUInt32(header, 54);
//            if (vault_size + 86 != fsvault->Length) return NL_STATUS_BADFILE;
//        }
//        catch (...) {
//            return NL_STATUS_UNKNOWN;
//        }
//
//        return NL_STATUS_OK;

        try {
            fsvault.seek(0);
            if (fsvault.read(header, 0, header_size) < header_size) return (int) PasswordManagerError.NL_STATUS_BADFILE;
            //System.out.println("here1" + header.length);

            version= BitConverter.toShort(header, 52);
            //System.out.println(version);
            if (version != 1) return (int) PasswordManagerError.NL_STATUS_VERSION;

            //System.out.println("here");

            vault_size = BitConverter.toInt(header, 54);
            if (vault_size + 86 != fsvault.length()) return (int) PasswordManagerError.NL_STATUS_BADFILE;
        }
        catch (Exception e) {
            return (int) PasswordManagerError.NL_STATUS_UNKNOWN;
        }

        return (int) PasswordManagerError.NL_STATUS_OK;
    }

    public VaultFile() {
        header_size = _MIN_HEADER_LENGTH;
        //header = gcnew array<Byte>(header_size);
        header = new byte[header_size];
        vault_size = 0;
        fsvault = null;
        flag_new = false;
    }

    public int create(String path) {
        return 0;
    }

    public void close() {

    }

    boolean is_open() {
        return fsvault != null;
    }

    boolean is_new() { return flag_new; }

    String get_vault_path() { return vaultpath; }


}
